﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using He.Common;
using He.BLL;
using System.Data;
using He.Common.dto;

namespace He.Web.api
{
    /// <summary>
    /// 新闻操作
    /// </summary>
    [RoutePrefix("api/article")]
    public class ArticleController : ApiController
    {
        /// <summary>
        /// 获取文章内容
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("{id}")]
        public Model.article GetArticle(int id)
        {
            return new BLL.article().GetModel(id);
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="isRed">是否推荐</param>
        /// <param name="isHot">是否热门</param>
        /// <param name="categoryId">分类Id</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="page">当前页</param>
        /// <returns></returns>
        [HttpGet, Route("{module}/list")]
        public Pagination<Model.article> GetList(string module, bool isRed = false, bool isHot = false, int categoryId = 0, int pageSize = 10, int page = 0)
        {
            BLL.article bll = new BLL.article();
            StringBuilder strWhere = new StringBuilder("");
            strWhere.AppendFormat(" category_id in(select id from {0}category where channel_id in(select id from {0}channel where name='{1}'))", SysKeys.TABLE_PREFIX, module);
            if (isRed)
            {
                strWhere.Append(" AND is_red=1");
            }
            if (isHot)
            {
                strWhere.Append(" AND is_hot=1");
            }
            if (categoryId > 0)
            {
                strWhere.AppendFormat(" AND category_id in(SELECT id FROM {0}category WHERE id_list like '%,{1},%')", SysKeys.TABLE_PREFIX, categoryId);
            }
            var count = bll.GetRecordCount(strWhere.ToString());
            var items = bll.DataTableToList(bll.GetPageList(pageSize, page, strWhere.ToString(), "sort_id asc,id desc").Tables[0]);
            return new Pagination<Model.article>(count, items);
        }
    }
}
